package lb3;
import static java.lang.Math.cos;
import static java.lang.Math.log;

/**
 * Created with IntelliJ IDEA.
 * User: Борис
 * Date: 28.10.13
 * Time: 19:37
 * To change this template use File | Settings | File Templates.
 */
public class LabThree {

    private double a;
    private double b;
    private int n;

    LabThree(double a, double b, int n){
        this.a = a;
        this.b = b;
        this.n = n;
    }

    LabThree(){
        this.a = 0;
        this.b = 0;
        this.n = 0;
    }
//=====================================================================================
    private double f(double x){
        return cos(log(x));
    }

//    double leftRectangles(){
//        double sum = 0;
//        double h = (b-a)/n;
//        for(int i = 0; i<n; i++){
//            sum += f(a+i*h);
//        }
//        return h*sum;
//    }
//
//    double RightRectangles(){
//        double sum = 0;
//        double h = (b-a)/n;
//        for(int i = 1; i<n+1; i++){
//            sum += f(a+i*h);
//        }
//        return h*sum;
//    }

    double mediumRectangles(){
        double sum = 0;
        double h = (b-a)/n;
        double medium = h/2.0+a;
        for(int i = 0; i<n; i++){
            sum += f(medium+i*h);
        }
        return h*sum;
    }

    double trapezoidalMethod(){
        double sum = 0;
        double h = (b-a)/n;
        for(int i = 1; i<n; i++){
            sum += f(a+i*h);
        }
        return h*(((f(a)+f(b))/2.0)+sum);
    }

    double MethodSimpsons(){
        double sum1 = 0;
        double sum2 = 0;
        double h = (b-a)/n;
        double medium = h/2.0+a;
        for(int i = 0; i<n; i++){
            sum1 += f(medium+i*h);
        }
        for(int i = 1; i<n; i++){
            sum2 += f(a+i*h);
        }
        double S1 = h/6.0;
        double S2 = 4*sum1;
        double S3 = 2*sum2;
        return S1 * ( f(a) + f(b) + S2 + S3);
    }
//=====================================================================================
//    double leftRectangles(double a, double b, int n){
//        double sum = 0;
//        double h = (b-a)/n;
//        for(int i = 0; i<n; i++){
//            sum += f(a+i*h);
//        }
//        return h*sum;
//    }
//
//    double RightRectangles(double a, double b, int n){
//        double sum = 0;
//        double h = (b-a)/n;
//        for(int i = 1; i<n+1; i++){
//            sum += f(a+i*h);
//        }
//        return h*sum;
//    }

    double mediumRectangles(double a, double b, int n){
        double sum = 0;
        double h = (b-a)/n;
        double medium = h/2.0+a;
        for(int i = 0; i<n; i++){
            sum += f(medium+i*h);
        }
        return h*sum;
    }

    double trapezoidalMethod(double a, double b, int n){
        double sum = 0;
        double h = (b-a)/n;
        for(int i = 1; i<n; i++){
            sum += f(a+i*h);
        }
        return h * ( ( f(a) + f(b) ) / 2.0 + sum);
    }

    double MethodSimpsons(double a, double b, int n){
        double sum1 = 0;
        double sum2 = 0;
        double h = (b-a)/n;
        double medium = h/2.0+a;
        for(int i = 0; i<n; i++){
            sum1 += f(medium+i*h);
        }
        for(int i = 1; i<n; i++){
            sum2 += f(a+i*h);
        }
        double S1 = h/6.0;
        double S2 = 4*sum1;
        double S3 = 2*sum2;
        return S1 * ( f(a) + f(b) + S2 + S3);
    }
//=====================================================================================
}
